package pe.org.manuela.app.ui

import groovy.swing.*
import java.awt.*
import javax.swing.*
import javax.swing.border.*
import javax.swing.table.*
import pe.org.manuela.app.despachadores.*

static def cargar(sw) {
	
	def obtenerInsumos = {
		def insumos = InsumoDespachador.obtenerInsumos()
		def tm = sw.grdInsumos.model
		insumos.each {
			tm.addRow([it.id, it, it.estado] as Object[])
		}		
	}
	
	def agregarInsumo = {
		InsumoUI.cargar(sw, false)		
	}
	
	def editarInsumo = {
			InsumoUI.cargar(sw, true)
			Thread.start {
				sw.grdInsumos.model.rowCount = 0
				obtenerInsumos()
			}		
	}
	
	def frmInsumos = sw.internalFrame(
		id: "frmInsumos",
		title: "Insumos",
		closable: true,
		iconifiable: true,
		maximizable: true,
		defaultCloseOperation: JFrame.DISPOSE_ON_CLOSE
		){
				borderLayout(hgap: 10, vgap: 10)
				panel(constraints: BorderLayout.CENTER) {
					boxLayout(axis: BoxLayout.X_AXIS)
					panel() {
						def columnas = ["Código", "Descripción", "Estado"] as String[]
						def tm = new DefaultTableModel(columnas, 0)
						scrollPane(preferredSize: [400, 300]) {
							table(id: "grdInsumos", model: tm)
						}
					}
					panel() {
						boxLayout(axis: BoxLayout.Y_AXIS)
						panel(preferredSize: [0,100])
						panel() {
							gridLayout(rows: 4, columns: 1, hgap: 10, vgap: 10)
							button(text: "Nuevo", actionPerformed: {
								agregarInsumo()
							})
							button(text: "Editar", actionPerformed: {
								editarInsumo()
							})
							button(text: "Eliminar", actionPerformed:{
								eliminarInsumo()
							})
							button(text: "Salir")
						}
						panel(preferredSize: [0,100])						
					}
				}
			
	}
	
	frmInsumos.pack()
	frmInsumos.visible = true
	sw.escritorio <<  frmInsumos
	
	Thread.start{
		obtenerInsumos()
	}
}
